//
//  ViewController.m
//  17-UI-AddressBook
//
//  Created by Snow on 16/8/5.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "MyTableViewCell.h"
#import "DetailViewController.h"
#import "Model.h"
#import "AppDelegate.h"
#import "AddViewController.h"
#import "SQLiteManager.h"

#define kReuse @"cell"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *arrayOfModel;
@property (nonatomic, strong) SQLiteManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  通过数据库实现通讯录添加联系人功能。
     需求:
     1、通讯录展示页面导航栏右侧,设置“添加”按钮。
     2、点击“添加”按钮能弹出联系人详细信息页面，要求输入信息UITextField 。
     3、联系人图片可以从相册选取。
     4、所有的信息一旦添加，则永久保存在数据库中，下次打开依然显示。
     5、联系人界面使用tableView展示。
     保存图片, 思路提供: 图片取出, 转成Data, 写入本地沙盒, 数据库中存Data对应的路径
     注意: 保证沙盒的干净整洁(Data存入文件夹)
     */

    _manager = [SQLiteManager sharedManger];
    [_manager openSQLite3WithFileName:@"DLS160710.sqlite"];
    [_manager createTabel];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleAdd:)];
    _arrayOfModel = [NSMutableArray array];
    NSString *path = @"/Users/Snow/DLS160710/UI/HomeWork/17-UI-AddressBook/17-UI-AddressBook/DLS160710.plist";
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in array) {
        Model *model = [[Model alloc] init];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithDictionary:dic];
        [dic1 setValue:[UIImage imageNamed:@"u=1563663233,4023384541&fm=23&gp=0"] forKey:@"image"];
        [model setValuesForKeysWithDictionary:dic1];
        [_arrayOfModel addObject:model];
        [_manager insertModel:model];
    }
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:kReuse];
    [_tableView reloadData];
    
}
- (void)handleAdd:(UIBarButtonItem *)button
{
    AddViewController *vc = [[AddViewController alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_arrayOfModel removeObjectAtIndex:indexPath.row];
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayOfModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    Model *model = [_arrayOfModel objectAtIndex:indexPath.row];
    cell.labelOfName.text = model.name;
    cell.labelOfPhone.text = model.phoneNumber;
    cell.imageView.image = model.image;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *vc = [[DetailViewController alloc] init];
    vc.model = [_arrayOfModel objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)addContact:(Model *)model
{
    [_manager insertModel:model];
    [_arrayOfModel addObject:model];
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
